package cn.zn.framework.common.utils;

import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.zn.framework.common.exception.RRException;
import cn.zn.framework.common.vo.FinanceValues;
import com.beust.jcommander.internal.Lists;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.Calendar;
import java.util.List;
import java.util.Random;

@Component
@Getter
@Slf4j
public class FileUtils {
    @Autowired
    private FinanceValues financeValues;

    /**
     * 得到带域名的路径
     *
     * @param path 文件相对路径
     * @return
     */
    public String getDomainUrl(String path) {
        return this.financeValues.getFile().getDomain().concat(path.replace(File.separator, "/"));
    }

    /**
     * 得到不带域名的路径
     *
     * @param path 文件的完整访问路径
     * @return
     */
    public String getNoDomainUrl(String path) {
        return path.substring(this.financeValues.getFile().getDomain().length());
    }

    /**
     * 得到相对路径
     *
     * @param path 文件的完整保存路径
     * @return
     */
    public String getNoBasePath(String path) {
        File baseDir = new File(this.financeValues.getFile().getBasePath());
        return path.substring(baseDir.getPath().trim().length());
    }

    /**
     * 得到带域名路径
     *
     * @param path 文件完整保存路径
     * @return
     */
    public String getDomainUrlForSavePath(String path) {
        return this.getDomainUrl(this.getNoBasePath(path));
    }

    /**
     * 得到保存文件的根目录
     *
     * @return
     */
    public String getSaveDir() {
        StrBuilder dirBuilder = StrBuilder.create(this.financeValues.getFile().getBasePath());
        dirBuilder.append(File.separator).append(this.financeValues.getFile().getUploadFiles());
        return dirBuilder.toString();
    }

    /**
     * 上传文件
     *
     * @param inputStream
     * @param suffix
     * @param subDirs
     * @return
     * @throws IOException
     */
    public File updateFile(InputStream inputStream, String suffix, Boolean thumbnail, String... subDirs) throws IOException {
        if (!StrUtil.isNotBlank(this.financeValues.getFile().getBasePath())) {
            throw new RRException("未设置文件保存路径");
        }
        StrBuilder dirBuilder = StrBuilder.create(this.getSaveDir());
        if (subDirs != null) {
            for (String subDir : subDirs) {
                dirBuilder.append(File.separator).append(subDir);
            }
        }
        String createName = String.valueOf(System.currentTimeMillis())
                .concat(ArrayUtil.join(NumberUtil.generateRandomNumber(0, 9, 3), ""));
        dirBuilder.append(File.separator).append(createName);
        if (StrUtil.isNotBlank(suffix)) {
            dirBuilder.append(".").append(suffix);
        }
        File outputFile = FileUtil.touch(dirBuilder.toString());
        BufferedOutputStream outputStream = FileUtil.getOutputStream(outputFile);
        IoUtil.copy(inputStream, outputStream);
        if (inputStream != null) {
            inputStream.close();
        }
        if (outputStream != null) {
            outputStream.close();
        }
        String type = FileTypeUtil.getType(outputFile);
        if (thumbnail && StrUtil.equalsAny(type, true, "jpg", "jpeg", "gif", "png")) {
            ImageUtil.generateThumbnail(outputFile, 100, 100);
        }
        return outputFile;
    }

    public File updateFile(InputStream inputStream, String suffix, String... subDirs) throws IOException {
        return this.updateFile(inputStream, suffix, true, subDirs);
    }

}
